import os
from concurrent.futures.thread import ThreadPoolExecutor
from concurrent.futures import wait
import string
import threading
import itertools
import time

import pymysql
host = "139.196.152.104"
db = "test2"
user = "sunyuming"
passwd = "memories"

need_str=string.digits+string.ascii_letters
print(len(need_str))
r=itertools.product(need_str,repeat=5)
list=[]


#分组:分成多少个文件
number=1000
thread_pool=ThreadPoolExecutor(number)
def go(list_xxx):
    jishu = 0
    for ss in list_xxx:
        try:
            conn = pymysql.connect(host=host, port=3306, db=ss, user=user, passwd=passwd, charset='utf8')
            print("成功了:"+ss)
            sys.exit()
            break
        except:
            print("失败次数", jishu)
            jishu += 1
def fenzu():
    #首先删除所有文件
    for f in os.listdir("list"):
        path_file=os.path.join("list",f)
        os.remove(path_file)
    for i in r:
        ss = "".join(i)
        list.append(ss)
    #价格
    jiange=int(len(list)/number)
    print("间隔",jiange)
    result_list=[list[x:x+jiange] for x in range(0,len(list),jiange)]
    print("分配数组完成",len(result_list))
    nn=0
    for x in result_list:
        print(str(nn))
        file_name=r"list/text"+str(nn)+".txt"
        with open(file_name, "a") as f:
            f.write("\n".join(x))
        nn+=1
    #task_list = [thread_pool.submit(go,list_xxx=list_xxx) for list_xxx in result_list]
    #print(wait(task_list))
def open(filename):
    with open(filename,"r") as f:
        f.readline()
if __name__ == '__main__':
    #首先将所有可能性保存到文件中
    fenzu()

"""



thread_pool.submit()
task_list=[thread_pool.submit(go) for l in list]
print(r.__sizeof__())

#根据设置的长度进行遍历
def get_list(number):
    pass
print(str)
success=True
while success:
    try:
        conn = pymysql.connect(host=host, port=3306, db=db, user=user, passwd=passwd, charset='utf8')
    except:
        print(1)
        
        """

